package servlet;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.lang.reflect.Method;
import java.util.Calendar;
import java.util.List;
import java.util.StringTokenizer;

import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;

import music.dao.impl.*;
import music.entity.*;


public class DoServlet extends HttpServlet {

	/**
	 * Constructor of the object.
	 */
	public DoServlet() {
		super();
	}

	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {this.doPost(request, response);}

	
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		
		String path = request.getServletPath();// 获得路径
		path = path.substring(1, path.length() - 3);
		System.out.println(path);
		try {
			if ("service".equals(path) || "doGet".equals(path)
					|| "doPost".equals(path)) {
				response.sendError(500, "地址禁止访问！");
				return;
			}
			Method m = this.getClass().getDeclaredMethod(path,
					HttpServletRequest.class, HttpServletResponse.class);
			m.invoke(this, request, response);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			response.sendError(500, "地址[" + path + "]不存在！");
		}

	}
	//以下是各个实现方法
	//
	public void index(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// 在跳到首页之前需要处理一些数据并携带
		//数据包括：试听排行榜，下载排行榜，试听量最大的四个专辑，新歌速递，热门歌手，试听前三的歌曲，所有分类；cookie
		//如果cookie有值的话，则根据用户信息获得对应的试听列表和下载列表并分别显示
		request.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		//试听排行数据
		List<Song> ls = MusicDaoImpl.DAO.listening();
		request.setAttribute("stph", ls);
		//下载排行
		List<Song> xz = MusicDaoImpl.DAO.download();
		request.setAttribute("xzph", xz);
		//试听量最大的四个专辑
		List<String> sj = MusicDaoImpl.DAO.searchTop();
		request.setAttribute("stzj", sj);
		//新歌速递
		List<Song> xd =MusicDaoImpl.DAO.searchNew();
		request.setAttribute("xgsd", xd);
		//热门歌手
		List<String> singerlist= MusicDaoImpl.DAO.geshou();
		request.setAttribute("singerlist", singerlist);
		//试听前三的歌曲
		List<Song> xs = MusicDaoImpl.DAO.showBy();
		request.setAttribute("xtqs",xs );
		//所有分类
		List<Topics> sl = CatalogDaoImpl.DAO.selTopics();
		request.setAttribute("topics", sl);
		//cookie
		Cookie[] cok = request.getCookies();
		String name = null,password = null;
		for (Cookie cookie : cok) {
			if(("name").equals(cookie.getName())){
				name = cookie.getValue();
			}
			if(("password").equals(cookie.getName())){
				password = cookie.getValue();
			}
		}
				try {
					if(name!=null&&password!=null){
					User uid = UserDaoImpl.DAO.login(name, password);
					if(uid.getUsername()!=null){
						Cookie cook1 = new Cookie("name", name);
						Cookie cook2 = new Cookie("password", password);
						
						cook1.setMaxAge(60*60*24*10);
						cook2.setMaxAge(60*60*24*10);
						response.addCookie(cook1);
						response.addCookie(cook2);
						request.setAttribute("user", uid);	
						List<Song> tg = MusicDaoImpl.DAO.listened(uid.getId() );
						request.setAttribute("listenlist", tg);
						//塞入用户下载过的歌曲列表
						List<Song> xg = MusicDaoImpl.DAO.downloaded(uid.getId());
						request.setAttribute("downloadlist", xg);
					}
}
				} catch (Exception e) {
					// TODO 自动生成的 catch 块
					e.printStackTrace();
					request.setAttribute("hint", "你还没有登录！");
				}

		request.getRequestDispatcher("/index.jsp").forward(request, response);
			
	}
		// 管理员和用户的登录方法
		public void login(HttpServletRequest request, HttpServletResponse response)
				throws ServletException, IOException {
			request.setCharacterEncoding("utf-8");
			response.setContentType("text/html;charset=utf-8");
			//这个方法负责对登录的信息进行处理
			String name = request.getParameter("name");
			String password = request.getParameter("password");
			String type = request.getParameter("identity");
			if(type.equals("admin")){
			Manager manager = AdminDaoImpl.DAO.login(name, password);	
			request.setAttribute("login", manager);
				request.getRequestDispatcher("/adminresult.jsp").forward(request, response);	
			return;
		}
		else if(type.equals("putong")){
			User manager = UserDaoImpl.DAO.login(name,password);

			request.setAttribute("user", manager);
			request.getRequestDispatcher("/IndexServlet.jsp").forward(request, response);
			}		
		}

		// 管理员或普通用户退出方法
		public void out(HttpServletRequest request, HttpServletResponse response)
				throws ServletException, IOException {
			request.setCharacterEncoding("utf-8");
			response.setContentType("text/html;charset=utf-8");
			//在转发到首页之前将coie注销
			Cookie [] cookie = request.getCookies();
			String name =null;
			String password = null;
			for (Cookie cookie2 : cookie) {
				if(("name").equals(cookie2.getName())){
					name = cookie2.getValue();
				}
				if(("password").equals(cookie2.getName())){
					password = cookie2.getValue();
				}
			}
			Cookie cook1 = new Cookie("name", name);
			Cookie cook2 = new Cookie("password", password);
			cook1.setMaxAge(0);
			cook2.setMaxAge(0);
			response.addCookie(cook1);
			response.addCookie(cook2);

			request.getSession().removeAttribute("login");
			request.getSession().setMaxInactiveInterval(0);
			
			request.getRequestDispatcher("/index.do").forward(request, response);
		}
		//用户登录成功后的的页面跳转
		public void user(HttpServletRequest request, HttpServletResponse response)
						throws ServletException, IOException {
				request.setCharacterEncoding("utf-8");
				response.setContentType("text/html;charset=utf-8");
				String name =request.getParameter("name");
				User user = UserDaoImpl.DAO.search(name);
				Cookie cook1 = new Cookie("name", name);
				Cookie cook2 = new Cookie("password", user.getPassword());
				cook1.setMaxAge(60*60*24*10);
				cook2.setMaxAge(60*60*24*10);
				response.addCookie(cook1);
				response.addCookie(cook2);
				request.setAttribute("user", user);
				request.getRequestDispatcher("/index.do").forward(request, response);
				}
		
		//管理员管理歌曲的页面跳转
		public void admin(HttpServletRequest request, HttpServletResponse response)
				throws ServletException, IOException {
			request.setCharacterEncoding("utf-8");
			response.setContentType("text/html;charset=utf-8");
			String name =request.getParameter("name");
			if(name!=null){
			Manager manager = AdminDaoImpl.DAO.search(name);
			request.getSession().setAttribute("login", manager);
			request.getSession().setMaxInactiveInterval(10*60);
			int pageNo = 1;
			try {
				pageNo = Integer.parseInt(request.getParameter("pageNo"));
			} catch (Exception e) {}
			request.setAttribute("topics", CatalogDaoImpl.DAO.selTopics());
			Page<Song> pagelist = MusicDaoImpl.DAO.search("", "", pageNo);
			request.setAttribute("pagelist", pagelist);
			request.setAttribute("pageNo", pageNo);
			request.getRequestDispatcher("/admin.jsp").forward(request, response);
			}
			else{
				request.setAttribute("error", "管理员登录错误或未登录！");
				request.getRequestDispatcher("/error.jsp").forward(request, response);
			}
		}
		//管理员管理歌曲的搜索跳转
				public void adminSearch(HttpServletRequest request, HttpServletResponse response)
						throws ServletException, IOException {
					request.setCharacterEncoding("utf-8");
					response.setContentType("text/html;charset=utf-8");
					int id= Integer.parseInt(request.getParameter("songType_more"));
					System.out.println("******>>>>****" + id);
					String search = request.getParameter("key"); 
					String choice = request.getParameter("fieldName");
					int pageNo = 1;
					try {
						pageNo = Integer.parseInt(request.getParameter("pageNo"));
					} catch (Exception e) {}
					Page<Song> pagelist = MusicDaoImpl.DAO.adminsearch(id,search, choice, pageNo);
					System.out.println(pagelist.getPageNums());
					request.setAttribute("topics", CatalogDaoImpl.DAO.selTopics());
					request.setAttribute("pagelist", pagelist);
					request.setAttribute("pageNo", pageNo);
					request.setAttribute("hint2", choice);
					request.setAttribute("hint1", search);
					request.setAttribute("id",id);
					request.getRequestDispatcher("/admin.jsp").forward(request, response);
				}
				//管理员页面
				//根据搜索框进行搜索后点击下一页
				public void adminsearchByNext(HttpServletRequest request, HttpServletResponse response)
								throws ServletException, IOException {
							request.setCharacterEncoding("utf-8");
							response.setContentType("text/html;charset=utf-8");
							int id=0;
							try {
								id=Integer.parseInt(request.getParameter("id"));
							} catch (Exception e1) {}
							String search = request.getParameter("hint1");
							String choice = request.getParameter("hint2");
							int pageNo = 1;
							try {
								pageNo = Integer.parseInt(request.getParameter("pageNo"));
							} catch (Exception e) {}
							Page<Song> pagelist = MusicDaoImpl.DAO.adminsearch(id,search, choice, pageNo);
							request.setAttribute("topics", CatalogDaoImpl.DAO.selTopics());
							request.setAttribute("pagelist", pagelist);
							request.setAttribute("pageNo", pageNo);
							request.setAttribute("hint2", choice);
							request.setAttribute("hint1", search);
							request.setAttribute("id",id);
							request.getRequestDispatcher("/admin.jsp").forward(request, response);
						}	
		//根据歌曲分类显示具体歌曲(a标记<a href="showByType.do?id = 3&topicname=${topic.name}&pageNo="+${pageNo} >欧美金曲 </a>)
		public void showByType(HttpServletRequest request, HttpServletResponse response)
				throws ServletException, IOException {
			request.setCharacterEncoding("utf-8");
			response.setContentType("text/html;charset=utf-8");
			int id =Integer.parseInt(request.getParameter("id"));
			int pageNo = 1;
			try {
				pageNo = Integer.parseInt(request.getParameter("pageNo"));
			} catch (Exception e) {}
			Page<Song> pagelist = MusicDaoImpl.DAO.songTopic(id, pageNo);
			request.setAttribute("pagelist", pagelist);
			request.setAttribute("pageNo", pageNo);
			request.setAttribute("id", id);
			request.setAttribute("hint", "歌曲种类》");
			request.getRequestDispatcher("/Typeshow.jsp").forward(request, response);
		}
		//根据搜索框进行搜索（）
		public void searchBy(HttpServletRequest request, HttpServletResponse response)
				throws ServletException, IOException {
			request.setCharacterEncoding("utf-8");
			response.setContentType("text/html;charset=utf-8");
			String search = request.getParameter("key");
			System.out.println(search.length());
			String choice = request.getParameter("fieldName");
			System.out.println(choice);
			int pageNo = 1;
			try {
				pageNo = Integer.parseInt(request.getParameter("pageNo"));
			} catch (Exception e) {}
			Page<Song> pagelist = MusicDaoImpl.DAO.search(search, choice, pageNo);
			System.out.println(pagelist.getPageNums());
			request.setAttribute("pagelist", pagelist);
			request.setAttribute("pageNo", pageNo);
			request.setAttribute("hint2", choice);
			request.setAttribute("hint1", search);
			request.getRequestDispatcher("/searchshow.jsp").forward(request, response);
		}
		//根据搜索框进行搜索后点击下一页
		public void searchByNext(HttpServletRequest request, HttpServletResponse response)
						throws ServletException, IOException {
					request.setCharacterEncoding("utf-8");
					response.setContentType("text/html;charset=utf-8");
					String search = request.getParameter("hint1");
					String choice = request.getParameter("hint2");
					int pageNo = 1;
					try {
						pageNo = Integer.parseInt(request.getParameter("pageNo"));
					} catch (Exception e) {}
					Page<Song> pagelist = MusicDaoImpl.DAO.search(search, choice, pageNo);
					request.setAttribute("pagelist", pagelist);
					request.setAttribute("pageNo", pageNo);
					request.setAttribute("hint2", choice);
					request.setAttribute("hint1", search);
					request.getRequestDispatcher("/searchshow.jsp").forward(request, response);
				}
		
		
		//点击专辑后分页显示对应的专辑内的歌曲
		//（<a href="showBySpecial.do?special="+${song.specialname}+"&pageno="+${pageno} >${song}</a>）
		public void showBySpecial(HttpServletRequest request, HttpServletResponse response)
				throws ServletException, IOException {
			request.setCharacterEncoding("utf-8");
			response.setContentType("text/html;charset=utf-8");
			String special = request.getParameter("special");
			System.out.println("asaa" + special);
			int pageNo = 1;
			try {
				pageNo = Integer.parseInt(request.getParameter("pageNo"));
			} catch (Exception e) {}
			Page<Song> pagelist = MusicDaoImpl.DAO.showByspecial(special,pageNo);
			request.setAttribute("pagelist", pagelist);
			request.setAttribute("pageNo", pageNo);
			request.setAttribute("hint", "专辑>>"+special+">>");
			request.getRequestDispatcher("/specialshow.jsp").forward(request, response);
		}
		//点击歌曲进入歌曲详情页面
		//<a href="SongShow.do?id=${song.id}" > ${song} <a>
		public void songShow(HttpServletRequest request, HttpServletResponse response)
				throws ServletException, IOException {
			request.setCharacterEncoding("utf-8");
			response.setContentType("text/html;charset=utf-8");
			int id = Integer.parseInt(request.getParameter("id"));
			int pageNo = 1;
			try {
				pageNo = Integer.parseInt(request.getParameter("pageNo"));
			} catch (Exception e) {}
			Page<Song> pagelist = MusicDaoImpl.DAO.showByid(id,pageNo);
			request.setAttribute("pagelist", pagelist);
			request.setAttribute("id", id);
			request.setAttribute("pageNo", pageNo);
			request.setAttribute("hint", "歌曲>>"+pagelist.getDatas().get(0).getSongName()+">>");
			request.getRequestDispatcher("/songshow.jsp").forward(request, response);
		}
		//点击歌手进入该歌手所有歌曲的页面
		//<a href="SongerShow.do?singer=${singer}" > ${singer} <a>
			public void SongerShow(HttpServletRequest request, HttpServletResponse response)
						throws ServletException, IOException {
				request.setCharacterEncoding("utf-8");
				response.setContentType("text/html;charset=utf-8");
					String singer = request.getParameter("singer");
					int pageNo = 1;
					try {
						pageNo = Integer.parseInt(request.getParameter("pageNo"));
					} catch (Exception e) {}
					Page<Song> pagelist = MusicDaoImpl.DAO.showBysinger(singer,pageNo);
					request.setAttribute("pagelist", pagelist);
					request.setAttribute("pageNo", pageNo);
					request.setAttribute("type", "singer");
					request.setAttribute("singer", singer);
					request.setAttribute("hint", "歌手>>"+singer+">>");
					request.getRequestDispatcher("/singershow.jsp").forward(request, response);
				}
/*
 * 以上都是首页的功能
 */
		/*
		 * 以下是其他的功能
		 */
		//歌曲文件上传
		
		
		
		//删除歌曲
			//<a href="deletesong.do?id = ${song.id}" >删除</a>
		public void deletesong(HttpServletRequest request, HttpServletResponse response)
					throws ServletException, IOException {
			request.setCharacterEncoding("utf-8");
			response.setContentType("text/html;charset=utf-8");
				int id = Integer.parseInt(request.getParameter("id"));
				int rows = MusicDaoImpl.DAO.delete(id);
				request.setAttribute("rows", rows);
				request.setAttribute("name", "删除歌曲");
				request.getRequestDispatcher("/result.jsp").forward(request, response);
			}
		//添加歌曲分类
		//<a href="addtopic.jsp">添加主题</a>
		//<form action="addtopic.do"....
		public void addtopic(HttpServletRequest request, HttpServletResponse response)
				throws ServletException, IOException {
			request.setCharacterEncoding("utf-8");
			response.setContentType("text/html;charset=utf-8");
			String topicname = request.getParameter("topicname");
			int rows = CatalogDaoImpl.DAO.addTopics(topicname);
			request.setAttribute("rows", rows);
			request.setAttribute("name", "增加歌曲分类");
			request.getRequestDispatcher("/result.jsp").forward(request, response);
		}
		//编辑歌曲分类（包括修改和删除）
		//<a href="ShowServlet.do">编辑主题</a>
		public void ShowServlet(HttpServletRequest request, HttpServletResponse response)
				throws ServletException, IOException {
			request.setCharacterEncoding("utf-8");
			response.setContentType("text/html;charset=utf-8");
			request.setAttribute("topics", CatalogDaoImpl.DAO.selTopics());
			
			request.getRequestDispatcher("/ShowServlet.jsp").forward(request, response);
		}
		
		//修改歌曲分类
		//<a href="updatetopic.do?id=${topic.id}">
		public void updatetopic(HttpServletRequest request, HttpServletResponse response)
				throws ServletException, IOException {
			request.setCharacterEncoding("utf-8");
			response.setContentType("text/html;charset=utf-8");
			int id = Integer.parseInt(request.getParameter("id"));
			String name = request.getParameter("topicname");
			int rows = CatalogDaoImpl.DAO.updateTopics(id, name);
			request.setAttribute("rows", rows);
			request.setAttribute("name", "修改歌曲分类");
			request.getRequestDispatcher("/result.jsp").forward(request, response);
		}
		//删除歌曲分类
		//<a href="deletetopic.do?id=${topic.id}">
		public void deletetopic(HttpServletRequest request, HttpServletResponse response)
				throws ServletException, IOException {
			request.setCharacterEncoding("utf-8");
			response.setContentType("text/html;charset=utf-8");
			int id = Integer.parseInt(request.getParameter("id"));
			int rows = CatalogDaoImpl.DAO.delTopics(id);
			request.setAttribute("rows", rows);
			request.setAttribute("name", "删除歌曲分类");
			request.getRequestDispatcher("/result.jsp").forward(request, response);
		}
		
		//歌曲试听
		//<a href="trylisten.do?id=${song.id}">
		public void trylisten(HttpServletRequest request, HttpServletResponse response)
				throws ServletException, IOException {
			request.setCharacterEncoding("utf-8");
			response.setContentType("text/html;charset=utf-8");
			int id = Integer.parseInt(request.getParameter("id"));
		String[] urlAndName = MusicDaoImpl.DAO.tryListen(id); // 该数组的第一个元素为歌曲名称，第二个元素为歌曲的文件名
		/** **************获取歌词************************* */
		String lrcRealPath = request.getRealPath("/");
		String mp3RealPath = lrcRealPath.substring(0, lrcRealPath.lastIndexOf("/") + 1)+ "music/" + urlAndName[1];
		request.setAttribute("realPath", mp3RealPath);
		lrcRealPath = lrcRealPath+ "music/"+ urlAndName[1].substring(0, urlAndName[1].lastIndexOf(".") + 1)+ "lrc"; // lrc文件路径
		File lrcFile = new File(lrcRealPath);
		MusicDaoImpl.DAO.holdoutAdd(id); // 将试听次数加1
		String content = "";
		int lineNumber = 0;
		if (lrcFile.exists()) {
			FileInputStream lrcf;
			try {
				lrcf = new FileInputStream(lrcRealPath);
				int rs = 0;
				byte[] data = new byte[lrcf.available()]; // available()方法可以不受阻塞地从此输入流中读取（或跳过）的估计剩余字节数
				while ((rs = lrcf.read(data)) > 0) {
					content += new String(data, 0, rs);
				}
				StringTokenizer st = new StringTokenizer(content, "\\[*\\]"); // 分析字符串中共包括多少个中括号对“[]”
				lineNumber = st.countTokens(); // 返回分析的结果
				// lineNumber=content.split("\\[*\\]").length;
				System.out.println("SSSSSSSSSSS:" + lineNumber);
				// System.out.println("歌词内容："+content);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		/** ********************************************* */

		request.setAttribute("lineNumber", lineNumber); // 保存歌词的行数
		request.setAttribute("lrcContent", content); // 保存歌词内容
		request.setAttribute("fileURL", urlAndName[1]); // 保存当前页的歌曲信息
		request.setAttribute("songName", urlAndName[0]); // 保存当前试听的歌曲名称
		request.getRequestDispatcher("/trylisten.jsp").forward(request, response);
	}
		
		// 连续播放
		public void continuePlay(HttpServletRequest request, HttpServletResponse response)
				throws ServletException, IOException {
			request.setCharacterEncoding("utf-8");
			response.setContentType("text/html;charset=utf-8");
			String ID[]=request.getParameterValues("playId"); //获取要播放的歌曲ID
			String playID = "";
			//将要播放的歌曲ID连接为一个以逗号分隔的字符串
			for (int i = 0; i <= ID.length-1; i++) {
				playID = playID + ID[i] + ",";
			}
			playID = playID.substring(0, playID.length() - 1);		//去除尾部的逗号
			String realPath = request.getRealPath("/");
			String url = request.getRequestURL().toString();
			url = url.substring(0, url.lastIndexOf("/") + 1) + "music/";
			System.out.println("要连接播放的歌曲ID：" + playID);
			List<Song> songlist = MusicDaoImpl.DAO.continuePlay(playID, url,
					"ORDER BY upTime DESC");
			request.setAttribute("songNameList", songlist); // 连续播放
			request.getRequestDispatcher("/continuePlay.jsp").forward(request, response);
		}
		// 下载
		public void download(HttpServletRequest request, HttpServletResponse response)
				throws ServletException, IOException {
			request.setCharacterEncoding("utf-8");
			response.setContentType("text/html;charset=utf-8");
			String from=request.getHeader("referer");
				int id = Integer.parseInt(request.getParameter("id"));
				String fileURL =  MusicDaoImpl.DAO.getFileURL(id);
				File file = new File(request.getRealPath("/music/" + fileURL));
				if (file.exists()) {
					request.setAttribute("fileURL", fileURL); // 保存歌曲的文件名
					 MusicDaoImpl.DAO.downloadAdd(id); // 添加下载次数
				} else {
					request.setAttribute("error", "很抱歉，该歌曲文件不存在！");
					request.getRequestDispatcher("/error.jsp").forward(request, response);
				}
				request.getRequestDispatcher("/download.jsp").forward(request, response);
			
		}
		//添加歌曲(<a>  </a>)
		public void addsong(HttpServletRequest request, HttpServletResponse response)
				throws ServletException, IOException {
			request.setCharacterEncoding("utf-8");
			response.setContentType("text/html;charset=utf-8");
			List<Topics> TopicList =CatalogDaoImpl.DAO.selTopics();
			request.setAttribute("TopicList", TopicList);
			request.getRequestDispatcher("/addmusic.jsp").forward(request, response);
		}
		
		//获得数据并添加至数据库
		public void addMusic(HttpServletRequest request, HttpServletResponse response)
				throws ServletException, IOException {
			request.setCharacterEncoding("utf-8");
			response.setContentType("text/html;charset=utf-8");
			DiskFileItemFactory factory = new DiskFileItemFactory();
			ServletFileUpload upload = new ServletFileUpload(factory);
			upload.setHeaderEncoding("utf-8");// 设置字符集
			List<FileItem> fields = null;
			try {
				fields = upload.parseRequest(request);
			} catch (FileUploadException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}// 拿到所有的域
			String picPath = super.getServletContext().getRealPath("/music");// 拿到的图片准备存到哪里（存放路径）
			Song song = new Song();
			for (FileItem f : fields) {
				if (f.isFormField()) {
					String sf = f.getFieldName();
					if (sf.equals("songTypeId")) {
						song.setSongTypeId(Integer.parseInt(f.getString("utf-8")));
					} else if (sf.equals("songName")) {
						song.setSongName(f.getString("utf-8"));
					} else if (sf.equals("singer")) {
						song.setSinger(f.getString("utf-8"));
						System.out.println(song.getSinger()+"xxxxxxxxxxxxx");
					} else if (sf.equals("specialName")) {
						song.setSpecialName(f.getString("utf-8"));
					}
				} else {
					if (f.getName() == "") {
						continue;
					}
					else if(f.getFieldName().equals("fileURL")){
						String fileURL = f.getName();
						String format = fileURL.substring(fileURL.indexOf(".")+1);
						Calendar ca=Calendar.getInstance();
						String fileName=String.valueOf(ca.getTimeInMillis())+"."+format;
						String size =String.valueOf(f.getSize()/1024/1024) ;
						try {
							if("mp3".equals(format) || "wma".equals(format)){
							f.write(new java.io.File(picPath, fileName));
							}
							} catch (Exception e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
						song.setFileURL(fileName);
						song.setFormat(format);
						song.setFileSize(size);
					}
					else if(f.getName() == "lrcFileURL"){
					String lrcFileURL = f.getName();
					try {
						f.write(new java.io.File(picPath, lrcFileURL));
					} catch (Exception e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					}
				}
			}
			int rows = MusicDaoImpl.DAO.addSong(song);
			request.setAttribute("rows", rows);
			request.setAttribute("name", "添加歌曲");
			request.getRequestDispatcher("/result.jsp").forward(request,
					response);
		}
		//注册！
		public void register(HttpServletRequest request, HttpServletResponse response)
				throws ServletException, IOException {
			request.setCharacterEncoding("utf-8");
			response.setContentType("text/html;charset=utf-8");
			String username=request.getParameter("name"); 
			String password=request.getParameter("password");
			int adduser = UserDaoImpl.DAO.register(username, password);
			System.out.println(adduser+"++++++++++++++++++++++++++++++++++++++++");
			request.setAttribute("reg", adduser);
			request.getRequestDispatcher("/regresult.jsp").forward(request, response);
		}

		
		
		
	
	public void init() throws ServletException {
		// Put your code here
	}

}
